<!-- 项目审批 -->
<template>
	<view style="margin-bottom: 100rpx">
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">入库码单</block></cu-custom>
		<mescroll-uni :down="downOption" @emptyclick="downCallback" @down="downCallback" :up="upOption" @up="upCallback"
		 :fixed="true">
			<view :style="{ paddingTop: customBarH+'px' }" class="mycard">
				<view v-for="item in cardList" :key="item.id" class="card-item">
					<uni-card is-full title="入库码单" is-shadow note="true" :extra="item.id.toString()" thumbnail="/static/img/md.svg">
						<view class="audit-card-content">
							<view class="uni-flex uni-row">
								<view class="flex-item-30">堆号</view>
								<view class="flex-item-70">{{item.heapNum}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-30">层号</view>
								<view class="flex-item-70">{{item.layerNum}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-30">码单号</view>
								<view class="flex-item-70">{{item.stakeNum}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-30">规格</view>
								<view class="flex-item-70">{{item.spec}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-30">数量</view>
								<view class="flex-item-70">{{item.qty}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-30">重量</view>
								<view class="flex-item-70">{{item.weight}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-30">卷号</view>
								<view class="flex-item-70">{{item.innerBatch}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-30">备注</view>
								<view class="flex-item-70">{{item.remark}}</view>
							</view>
						</view>

						<template v-slot:footer>
							<view class="footer-box">
								<view class="cuIcon-close text-red" @tap="deleteClick(item)">删除</view>
								<view class="cuIcon-edit text-olive" @tap="editClick(item)">修改</view>
							</view>
						</template>
					</uni-card>
				</view>
			</view>
		</mescroll-uni>
		<view v-if="false">
			<uni-fab
				:pattern="pattern"
				:horizontal="horizontal"
				:vertical="vertical"
				:direction="direction"
				:popMenu="false"
				@fabClick="createDetail()">
			</uni-fab>
		</view>
	</view>
</template>

<script>
	import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue"
	import uniCard from "@/components/uni-card/uni-card"
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'

	import {
		mapGetters
	} from 'vuex'
	import {
		getRandomArrayElements,
		filePreview,
		formatStatus
	} from '@/utils/index.js'
	import { formatDate } from '@/utils/datetime.js'
	export default {
		components: {
			MescrollUni,
			uniCard,
			uniTag,
			uniIcons
		},
		computed: mapGetters(['user']),
		data() {
			return {
				mescroll: null,
				isPass: false,
				goodsInBillId: 0,
				detailId: 0,
				// 下拉刷新的常用配置
				downOption: {
					use: false, // 是否启用下拉刷新; 默认true
					auto: true, // 是否在初始化完毕之后自动执行下拉刷新的回调; 默认true
				},
				// 上拉加载的常用配置
				upOption: {
					use: false, // 是否启用上拉加载; 默认true
					auto: true, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
					page: {
						num: 1, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					},
					toTop: {
						src: '/static/img/backtop.png'
					},
					textNoMore: '没有更多数据了'
				},
				// 列表数据
				cardList: [],
				horizontal: 'right',
				vertical: 'top',
				direction: 'horizontal',
				pattern: {
					color: '#7A7E83',
					backgroundColor: '#fff',
					selectedColor: '#007AFF',
					buttonColor: '#007AFF'
				},
				qty: 0,
				weight: 0,
				customBarH:this.CustomBar
			}
		},
		onLoad(options) {
			try {
				this.goodsInBillId = options.goodsInBillId
				this.detailId = options.detailId
				this.qty=options.qty
				this.weight=options.weight

			} catch (e) {
				this.goodsInBillId = 0
				this.detailId = 0
			}
		},
		onShow() {
			/**
			 * todo: api请求分页查询项目待办
			 */
			uni.showLoading({
				title: '正在查询数据...'
			})
			/**
			 * todo: api请求分页查询项目待办
			 */
			this.$minApi.goodsInBill(this.goodsInBillId).then(res => {
				this.cardList = []
				const data = res
				if (data) {
					const detailId = Number(this.detailId)
					const detail =  data.goodsInDetails.find(f => f.id === detailId)
					if (detail) {
						this.cardList = detail.goodsInStacks
					}						
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				// mescroll.endErr()
				// 隐藏加载状态
				uni.hideLoading()
			})
		},
		methods: {
			filePreview,
			formatStatus,
			formatDate,
			// mescroll组件初始化的回调,可获取到mescroll对象
			mescrollInit(mescroll) {
				// 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
				this.mescroll = mescroll
			},
			/*下拉刷新的回调, 有三种处理方式: */
			downCallback(mescroll) {
				// 第2种: 下拉刷新和上拉加载调同样的接口, 那以上请求可删, 直接用mescroll.resetUpScroll()代替
				mescroll.resetUpScroll() // 重置列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			},
			/*上拉加载的回调*/
			upCallback(mescroll) {
				
				/**
				 * todo: api请求分页查询项目待办
				 */
				this.$minApi.goodsInBill(this.goodsInBillId).then(res => {
					this.cardList = []
					const data = res
					if (data) {
						const detailId = Number(this.detailId)
						const detail =  data.goodsInDetails.find(f => f.id === detailId)
						if (detail) {
							this.cardList = detail.goodsInStacks
						}						
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
					mescroll.endErr()
				})

				// // 此时mescroll会携带page的参数:
				// let pageNum = mescroll.num // 页码, 默认从1开始
				// let pageSize = mescroll.size // 页长, 默认每页10条

				// this.$minApi.pageQueryProjectTodo({}, pageNum, pageSize).then(res => {
				// 	// 接口返回的当前页数据列表 (数组)
				// 	let curPageData = res.rows
				// 	// 接口返回的总页数 (比如列表有26个数据,每页10条,共3页; 则totalPage值为3)
				// 	let totalPage = res.totalPageCount
				// 	// 接口返回的总数据量(比如列表有26个数据,每页10条,共3页; 则totalSize值为26)
				// 	let totalSize = res.total
				// 	// 接口返回的是否有下一页 (true/false)
				// 	let hasNext = res.hasNextPage

				// 	if (mescroll.num == 1) this.cardList = [] //如果是第一页需手动置空列表
				// 	this.cardList = this.cardList.concat(curPageData) //追加新数据

				// 	// 成功隐藏下拉加载状态
				// 	// 方法一(推荐): 后台接口有返回列表的总页数 totalPage
				// 	mescroll.endByPage(curPageData.length, totalPage)
				// 	this.$nextTick(() => {
				// 		mescroll.endSuccess(curPageData.length)
				// 	})
				// }).catch(() => {
				// 	// 失败隐藏下拉加载状态
				// 	mescroll.endErr()
				// })
			},
			deleteClick(item) {
				const _this = this
				uni.showModal({
				    title: '删除码单',
				    content: '正在删除入库码单，是否删除？',
				    success: function (res) {
				        if (res.confirm) {
				            _this.$minApi.deleteGoodsInStack(item.id).then(res => {
				            	uni.showToast({
				            	    title: '删除成功',
				            	    duration: 2000
				            	})
								  
								  var itemIndex = _this.cardList.findIndex(f => f.id === item.id)
								  if (itemIndex > -1) {
									_this.cardList.splice(itemIndex, 1)
								  }
	
				            }).catch(err => {
				            	console.log(err)
				            }).finally(() => {
				            })
				        }
				    }
				})
			},
			editClick(item) {
				uni.redirectTo({
					url: `/pages/todo/goodsInBill/editStack?goodsInBillId=${this.goodsInBillId}&detailId=${this.detailId}&stackId=${item.id}`
				})
			},
			createDetail() {
				uni.redirectTo({
					url: `/pages/todo/goodsInBill/editStack?goodsInBillId=${this.goodsInBillId}&detailId=${this.detailId}&stackId=0&qty=${this.qty}&weight=${this.weight}`
				})
			},
			queryByName(name) {
				uni.showLoading({
					title: '正在查询数据...'
				})
				/**
				 * todo: api请求查询项目待办
				 */
				setTimeout(() => {
					this.cardList = getRandomArrayElements(this.cardList, 1)
					uni.hideLoading()
				}, 3000)

				// this.$minApi.queryProjectTodo({
				// 	projectName: name
				// }).then(res => {
				// 	uni.hideLoading()
				// 	this.cardList = res.rows
				// })
			},
			updateQuery() {
				// 无效
				// if (this.mescroll !== null) {
				// 	this.downCallback(this.mescroll)
				// }
				this.queryByName('')
			},
			backedPage() {
				uni.redirectTo({
					url: `/pages/todo/goodsInBill/detail?id=${this.goodsInBillId}&currDetailId=${this.detailId}`
				})
			}
		}
	}
</script>

<style scoped>
	.mescroll-totop {
		opacity: 1 !important;
	}
	/deep/ .uni-fab__circle--rightTop {
		top: 90px !important;
	}
</style>
